<template>
	<div>
		<el-dialog :title="$t('action.add')" width="70%" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form" :model="form" :rules="rules" :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_name')" prop="name">
							<el-input v-model="form.name" autoComplete="off" clearable  :placeholder="$t('system.pleaseInput',{title:$t('field.Api_name')})"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_api_name')" prop="api_name">
							<el-input v-model="form.api_name" autoComplete="off" clearable  :placeholder="$t('system.pleaseInput',{title:$t('field.Api_api_name')})"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
				   <el-col :span="24">
                        <el-form-item :label="$t('field.Api_type')" prop="type">
                            <el-select style="width:100%" v-model="form.type" :size="size" clearable filterable :placeholder="$t('system.pleaseSelect')">
                                <el-option key="0"  label="POST" value="2"></el-option>
                                <el-option key="1"  label="GET" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                   </el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_return_type')" prop="return_type">
							<el-input v-model="form.return_type" autoComplete="off" clearable  :placeholder="$t('system.pleaseInput',{title:$t('field.Api_return_type')})"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				<el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_parameters')" prop="parameters">
							<draggable v-model="form.parameters" v-bind="{group:'item'}" handle=".parameters-handle">
							<el-row v-for="(item,i) in form.parameters" :key="i">
								<el-col :span="4">
							    <el-form-item class="activeItem">
                                        <el-select style="position:relative;left:10px;" v-model="item.request" size="small" clearable placeholder="请选择">
                                            <el-option key="1"  label="RequestBody" value="RequestBody"></el-option>
                                            <el-option key="2"  label="RequestParam" value="RequestParam"></el-option>
                                        </el-select>   
								</el-form-item>
								</el-col>
								<el-col :span="4">
							    <el-form-item class="activeItem">
									<el-input style="position:relative;left:5px;" v-model="item.name" placeholder="参数名"/>
								</el-form-item>
								</el-col>
								<el-col :span="4">
							    <el-form-item class="activeItem">
									<el-input style="position:relative;left:5px;" v-model="item.datatype" placeholder="参数类型"/>
								</el-form-item>
								</el-col>
								<el-col :span="3">
							    <el-form-item class="activeItem">
									<el-select style="position:relative;left:10px;" v-model="item.required" size="small" clearable placeholder="是否必须">
										<el-option key="1"  label="是" value="1"></el-option>
										<el-option key="2"  label="否" value="0"></el-option>
									</el-select> 
								</el-form-item>
								</el-col>
								<el-col :span="3">
							    <el-form-item class="activeItem">
									<el-input style="position:relative;left:5px;" v-model="item.defaultValue" placeholder="默认值"/>
								</el-form-item>
								</el-col>
								<el-col :span="4">
									<el-form-item class="activeItem">
										<el-input style="position:relative;left:5px;" v-model="item.explain" placeholder="参数说明"/>
									</el-form-item>
								</el-col>
								<el-col :span="2">
									<el-button type="danger" size="mini" style="position:relative;left:15px"  icon="el-icon-close" @click="deleteItem('parameters',i)"></el-button>
									<el-button class="parameters-handle" type="success" size="mini" style="position:relative;left:15px" icon="el-icon-rank"></el-button>
								</el-col>
							</el-row>
							</draggable>
							<div>
								<el-button type="info" icon="el-icon-plus" style="padding:5px 7px" size="mini" @click="addItem('parameters')">{{$t('action.add')}}</el-button>
								<el-button v-if="form.parameters.length > 0" type="warning" icon="el-icon-delete" style="padding:5px 7px" size="mini" @click="clearItem('parameters')">{{$t('action.clear')}}</el-button>
							</div>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_remark')" prop="remark">
							<el-input v-model="form.remark" autoComplete="off" clearable  :placeholder="$t('system.pleaseInput',{title:$t('field.Api_remark')})"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 				 <el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_api_auth')" prop="api_auth">
							<el-radio-group v-model="form.api_auth">
                                <el-radio :label="1">是</el-radio>
                                <el-radio :label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				 </el-row> 
				 <el-row>
					<el-form-item :label="$t('field.Api_action_type')" prop="action_type">
						<el-select style="width:100%" v-model="form.action_type"  filterable :placeholder="$t('system.pleaseSelect')">
							<el-option v-for="(item,i) in action" :key="i" :label="item.name" :value="item.type"></el-option>
						</el-select>
					</el-form-item>
				</el-row> 
				<el-row >
					<el-col :span="24">
						<el-form-item :label="$t('field.Api_other_config')" prop="other_config">
							<el-input type="textarea" :rows="4" v-model="form.other_config" autoComplete="off" clearable  :placeholder="$t('system.pleaseInput',{title:$t('field.Api_other_config')})"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
	 	 	 				</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">{{$t('system.confirm')}}</span>
					<span v-else>{{$t('system.submitting')}}</span>
				</el-button>
				<el-button :size="size" @click="closeForm">{{$t('system.cancel')}}</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { add } from '@/api/admin/sysapi'
import draggable from "vuedraggable"


export default {
	name:'apiadd',
	components: {
	   		draggable,
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
		action: {
			type: Array,
		}
	},
	data(){
		return {
			form: {
			   menu_id:this.$route.query.menu_id,
			   name:'',
			   api_name:'',
			   type:'2',
			   return_type:'Map<String,Object>',
			   parameters:[],
			   remark:'',
			   api_auth:1,
			   other_config:'',
			   create_time:'',
			   update_time:'',
			   action_type:'',
			},
			loading:false,
			rules: {
			}
		}
	},
	watch:{
		show(val){
			if(val){
			}
		}
	},
	methods: {
		open(){
		},
		submit(){
			this.$refs['form'].validate(valid => {
				if(valid) {
					this.loading = true
					add(this.form).then(res => {
						if(res.status == 200){
							this.$message({message: this.$t('system.success'), type: 'success'})
							this.$emit('refresh_list')
							this.closeForm()
						}
					}).catch(()=>{
						this.loading = false
					})
				}
			})
		},
		addItem(key){
			this.form[key].push({})
		},
		deleteItem(key,index){
			this.form[key].splice(index,1)
		},
		clearItem(key){
			this.form[key] = []
		},
		closeForm(){
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form']!==undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common.scss'
</style>
